package com.aifeng.hassan_food.mine

import android.os.Bundle
import android.view.View
import com.aifeng.base.BaseActivity
import com.aifeng.base.addItems
import com.aifeng.base.loadLinear
import com.aifeng.base.refresh
import com.aifeng.common_ui.base.gone
import com.aifeng.common_ui.base.oneClick
import com.aifeng.common_ui.base.visible
import com.aifeng.common_ui.helper.ActivityStack
import com.aifeng.common_ui.helper.getString
import com.aifeng.hassan_food.R
import com.aifeng.model.CommonData
import com.aifeng.utils.bindLifeCycle
import com.aifeng.utils.getExtra
import com.aifeng.utils.subscribeByFinally
import com.aifeng.viewmodel.AdressViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import com.jeremyliao.liveeventbus.utils.observe
import kotlinx.android.synthetic.main.activity_address.*
import kotlinx.android.synthetic.main.layout_empty.*
import kotlinx.android.synthetic.main.layout_list.*
import net.idik.lib.slimadapter.SlimAdapter
import org.jetbrains.anko.startActivity
import org.koin.androidx.viewmodel.ext.android.getViewModel

class AddressActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_address)
        initTitle(title = "收货地址")

        swipe_refresh.isRefreshing = true
        getData(pageNum)
    }

    override fun getViewModel() = getViewModel<AdressViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        empty_hint.text = "暂无相关收货地址信息！"
        swipe_refresh.refresh { getData(1) }
        recycle_list.loadLinear(baseContext, swipe_refresh) {
            if (!isLoadingMore) {
                isLoadingMore = true
                getData(pageNum)
            }
        }
        mAdapter = SlimAdapter.create()
            .register<CommonData>(R.layout.item_adress_list) { inner, jector ->
                jector.text(R.id.item_adress_name, inner.receiver)
                    .text(R.id.item_adress_phone, inner.phone)
                    .text(
                        R.id.item_adress_addr,
                        when {
                            inner.pro in inner.address
                                    || inner.city in inner.address
                                    || inner.area in inner.address -> inner.address
                            else -> inner.pro + inner.city + inner.area + inner.address
                        }
                    )
                    .visibility(
                        R.id.item_adress_default,
                        if (inner.type == "1") View.VISIBLE else View.GONE
                    )
                    .clicked(R.id.item_adress_edit) {
                        startActivity<AddressAddActivity>("data" to inner)
                    }
                    .clicked(R.id.item_adress) {
                        if (intent.getExtra("model") == "select") {
                            val items = listOf(
                                "check",
                                inner.id,
                                inner.receiver,
                                inner.phone,
                                when {
                                    inner.pro in inner.address
                                            || inner.city in inner.address
                                            || inner.area in inner.address -> inner.address
                                    else -> inner.pro + inner.city + inner.area + inner.address
                                }
                            )
                            LiveEventBus.get("adressConfirm").post(items.joinToString(","))
                            ActivityStack.screenManager.popActivities(this::class.java)
                        }
                    }
            }
            .attachTo(recycle_list)

        adress_add.oneClick { startActivity<AddressAddActivity>() }

        observe("adress") { updateList() }
    }

    private fun updateList() {
        swipe_refresh.isRefreshing = true

        empty_view.gone()
        if (mList.isNotEmpty()) {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        pageNum = 1
        getData(pageNum)
    }

    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel().getList(index, getString("userId"))
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = {
                    mList.apply {
                        if (index == 1) {
                            clear()
                            pageNum = index
                        }
                        addItems(it)
                        if (com.aifeng.base.count(it) > 0) pageNum++
                    }

                    mAdapter.updateData(mList)
                },
                onFinally = {
                    swipe_refresh.isRefreshing = false
                    isLoadingMore = false
                    empty_view.apply { if (mList.isEmpty()) visible() else gone() }
                }
            )
    }

}
